﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using Pharmacy.Entities;
using Pharmacy.Data;

#endregion

namespace Pharmacy.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="PoDetailsProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class PoDetailsProviderBaseCore : EntityProviderBase<Pharmacy.Entities.PoDetails, Pharmacy.Entities.PoDetailsKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, Pharmacy.Entities.PoDetailsKey key)
		{
			return Delete(transactionManager, key.PoDetailId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_poDetailId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int64 _poDetailId)
		{
			return Delete(null, _poDetailId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_poDetailId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int64 _poDetailId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_DrugDispo key.
		///		FK_PODetails_DrugDispo Description: 
		/// </summary>
		/// <param name="_drugId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByDrugId(System.String _drugId)
		{
			int count = -1;
			return GetByDrugId(_drugId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_DrugDispo key.
		///		FK_PODetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		/// <remarks></remarks>
		public TList<PoDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId)
		{
			int count = -1;
			return GetByDrugId(transactionManager, _drugId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_DrugDispo key.
		///		FK_PODetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId, int start, int pageLength)
		{
			int count = -1;
			return GetByDrugId(transactionManager, _drugId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_DrugDispo key.
		///		fkPoDetailsDrugDispo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_drugId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByDrugId(System.String _drugId, int start, int pageLength)
		{
			int count =  -1;
			return GetByDrugId(null, _drugId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_DrugDispo key.
		///		fkPoDetailsDrugDispo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_drugId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByDrugId(System.String _drugId, int start, int pageLength,out int count)
		{
			return GetByDrugId(null, _drugId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_DrugDispo key.
		///		FK_PODetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public abstract TList<PoDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_PO key.
		///		FK_PODetails_PO Description: 
		/// </summary>
		/// <param name="_poId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByPoId(System.String _poId)
		{
			int count = -1;
			return GetByPoId(_poId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_PO key.
		///		FK_PODetails_PO Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_poId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		/// <remarks></remarks>
		public TList<PoDetails> GetByPoId(TransactionManager transactionManager, System.String _poId)
		{
			int count = -1;
			return GetByPoId(transactionManager, _poId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_PO key.
		///		FK_PODetails_PO Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_poId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByPoId(TransactionManager transactionManager, System.String _poId, int start, int pageLength)
		{
			int count = -1;
			return GetByPoId(transactionManager, _poId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_PO key.
		///		fkPoDetailsPo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_poId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByPoId(System.String _poId, int start, int pageLength)
		{
			int count =  -1;
			return GetByPoId(null, _poId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_PO key.
		///		fkPoDetailsPo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_poId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public TList<PoDetails> GetByPoId(System.String _poId, int start, int pageLength,out int count)
		{
			return GetByPoId(null, _poId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PODetails_PO key.
		///		FK_PODetails_PO Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_poId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.PoDetails objects.</returns>
		public abstract TList<PoDetails> GetByPoId(TransactionManager transactionManager, System.String _poId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override Pharmacy.Entities.PoDetails Get(TransactionManager transactionManager, Pharmacy.Entities.PoDetailsKey key, int start, int pageLength)
		{
			return GetByPoDetailId(transactionManager, key.PoDetailId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_PODetails index.
		/// </summary>
		/// <param name="_poDetailId"></param>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.PoDetails"/> class.</returns>
		public Pharmacy.Entities.PoDetails GetByPoDetailId(System.Int64 _poDetailId)
		{
			int count = -1;
			return GetByPoDetailId(null,_poDetailId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PODetails index.
		/// </summary>
		/// <param name="_poDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.PoDetails"/> class.</returns>
		public Pharmacy.Entities.PoDetails GetByPoDetailId(System.Int64 _poDetailId, int start, int pageLength)
		{
			int count = -1;
			return GetByPoDetailId(null, _poDetailId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PODetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_poDetailId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.PoDetails"/> class.</returns>
		public Pharmacy.Entities.PoDetails GetByPoDetailId(TransactionManager transactionManager, System.Int64 _poDetailId)
		{
			int count = -1;
			return GetByPoDetailId(transactionManager, _poDetailId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PODetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_poDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.PoDetails"/> class.</returns>
		public Pharmacy.Entities.PoDetails GetByPoDetailId(TransactionManager transactionManager, System.Int64 _poDetailId, int start, int pageLength)
		{
			int count = -1;
			return GetByPoDetailId(transactionManager, _poDetailId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PODetails index.
		/// </summary>
		/// <param name="_poDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.PoDetails"/> class.</returns>
		public Pharmacy.Entities.PoDetails GetByPoDetailId(System.Int64 _poDetailId, int start, int pageLength, out int count)
		{
			return GetByPoDetailId(null, _poDetailId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PODetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_poDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.PoDetails"/> class.</returns>
		public abstract Pharmacy.Entities.PoDetails GetByPoDetailId(TransactionManager transactionManager, System.Int64 _poDetailId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;PoDetails&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;PoDetails&gt;"/></returns>
		public static TList<PoDetails> Fill(IDataReader reader, TList<PoDetails> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				Pharmacy.Entities.PoDetails c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("PoDetails")
					.Append("|").Append((System.Int64)reader[((int)PoDetailsColumn.PoDetailId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<PoDetails>(
					key.ToString(), // EntityTrackingKey
					"PoDetails",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new Pharmacy.Entities.PoDetails();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.PoDetailId = (System.Int64)reader[((int)PoDetailsColumn.PoDetailId - 1)];
					c.PoId = (System.String)reader[((int)PoDetailsColumn.PoId - 1)];
					c.DrugId = (System.String)reader[((int)PoDetailsColumn.DrugId - 1)];
					c.Quantity = (System.Int32)reader[((int)PoDetailsColumn.Quantity - 1)];
					c.RemainQuantity = (System.Int32)reader[((int)PoDetailsColumn.RemainQuantity - 1)];
					c.Remark = (reader.IsDBNull(((int)PoDetailsColumn.Remark - 1)))?null:(System.String)reader[((int)PoDetailsColumn.Remark - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="Pharmacy.Entities.PoDetails"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.PoDetails"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, Pharmacy.Entities.PoDetails entity)
		{
			if (!reader.Read()) return;
			
			entity.PoDetailId = (System.Int64)reader[((int)PoDetailsColumn.PoDetailId - 1)];
			entity.PoId = (System.String)reader[((int)PoDetailsColumn.PoId - 1)];
			entity.DrugId = (System.String)reader[((int)PoDetailsColumn.DrugId - 1)];
			entity.Quantity = (System.Int32)reader[((int)PoDetailsColumn.Quantity - 1)];
			entity.RemainQuantity = (System.Int32)reader[((int)PoDetailsColumn.RemainQuantity - 1)];
			entity.Remark = (reader.IsDBNull(((int)PoDetailsColumn.Remark - 1)))?null:(System.String)reader[((int)PoDetailsColumn.Remark - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="Pharmacy.Entities.PoDetails"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.PoDetails"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, Pharmacy.Entities.PoDetails entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.PoDetailId = (System.Int64)dataRow["PODetailId"];
			entity.PoId = (System.String)dataRow["POId"];
			entity.DrugId = (System.String)dataRow["DrugId"];
			entity.Quantity = (System.Int32)dataRow["Quantity"];
			entity.RemainQuantity = (System.Int32)dataRow["RemainQuantity"];
			entity.Remark = Convert.IsDBNull(dataRow["Remark"]) ? null : (System.String)dataRow["Remark"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.PoDetails"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">Pharmacy.Entities.PoDetails Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, Pharmacy.Entities.PoDetails entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region DrugIdSource	
			if (CanDeepLoad(entity, "DrugDispo|DrugIdSource", deepLoadType, innerList) 
				&& entity.DrugIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.DrugId;
				DrugDispo tmpEntity = EntityManager.LocateEntity<DrugDispo>(EntityLocator.ConstructKeyFromPkItems(typeof(DrugDispo), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.DrugIdSource = tmpEntity;
				else
					entity.DrugIdSource = DataRepository.DrugDispoProvider.GetByDrugId(transactionManager, entity.DrugId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DrugIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.DrugIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DrugDispoProvider.DeepLoad(transactionManager, entity.DrugIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion DrugIdSource

			#region PoIdSource	
			if (CanDeepLoad(entity, "Po|PoIdSource", deepLoadType, innerList) 
				&& entity.PoIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.PoId;
				Po tmpEntity = EntityManager.LocateEntity<Po>(EntityLocator.ConstructKeyFromPkItems(typeof(Po), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.PoIdSource = tmpEntity;
				else
					entity.PoIdSource = DataRepository.PoProvider.GetByPoId(transactionManager, entity.PoId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PoIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.PoIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PoProvider.DeepLoad(transactionManager, entity.PoIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion PoIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the Pharmacy.Entities.PoDetails object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">Pharmacy.Entities.PoDetails instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">Pharmacy.Entities.PoDetails Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, Pharmacy.Entities.PoDetails entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region DrugIdSource
			if (CanDeepSave(entity, "DrugDispo|DrugIdSource", deepSaveType, innerList) 
				&& entity.DrugIdSource != null)
			{
				DataRepository.DrugDispoProvider.Save(transactionManager, entity.DrugIdSource);
				entity.DrugId = entity.DrugIdSource.DrugId;
			}
			#endregion 
			
			#region PoIdSource
			if (CanDeepSave(entity, "Po|PoIdSource", deepSaveType, innerList) 
				&& entity.PoIdSource != null)
			{
				DataRepository.PoProvider.Save(transactionManager, entity.PoIdSource);
				entity.PoId = entity.PoIdSource.PoId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region PoDetailsChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>Pharmacy.Entities.PoDetails</c>
	///</summary>
	public enum PoDetailsChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>DrugDispo</c> at DrugIdSource
		///</summary>
		[ChildEntityType(typeof(DrugDispo))]
		DrugDispo,
			
		///<summary>
		/// Composite Property for <c>Po</c> at PoIdSource
		///</summary>
		[ChildEntityType(typeof(Po))]
		Po,
		}
	
	#endregion PoDetailsChildEntityTypes
	
	#region PoDetailsFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;PoDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PoDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PoDetailsFilterBuilder : SqlFilterBuilder<PoDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PoDetailsFilterBuilder class.
		/// </summary>
		public PoDetailsFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PoDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PoDetailsFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PoDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PoDetailsFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PoDetailsFilterBuilder
	
	#region PoDetailsParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;PoDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PoDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PoDetailsParameterBuilder : ParameterizedSqlFilterBuilder<PoDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PoDetailsParameterBuilder class.
		/// </summary>
		public PoDetailsParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PoDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PoDetailsParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PoDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PoDetailsParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PoDetailsParameterBuilder
	
	#region PoDetailsSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;PoDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PoDetails"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class PoDetailsSortBuilder : SqlSortBuilder<PoDetailsColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PoDetailsSqlSortBuilder class.
		/// </summary>
		public PoDetailsSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion PoDetailsSortBuilder
	
} // end namespace
